<style>
.app-main{
  background: #f5f9ff;
}
@media print {
  .hide-on-print {
    display: none;
  }
}
</style>
<template>
  <div class="app-container" style="margin: auto; margin-top: 20px; border-radius:5px; width: 80%;  background: #FFFFFF" >
    <el-row :gutter="10" class="mb8">
      <el-button  type="primary" @click="handleAdd" plain>开单</el-button>
      <el-button  type="primary" @click="hd" plain>合单</el-button>
      <el-button  type="primary" @click="chaidan" plain>拆单</el-button>
      <el-button plain>导入</el-button>
      <el-button @click="handleExport" plain>导出</el-button>
      <el-button plain @click="dayin">打印</el-button>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="订单状态" prop="status" >
        <el-select v-model="queryParams.status" clearable placeholder="请选择">
          <el-option label="全部" value=""></el-option>
          <el-option label="待审核" value="待审核"></el-option>
          <el-option label="待调度" value="待调度"></el-option>
          <el-option label="运输中" value="运输中"></el-option>
          <el-option label="已签收" value="已签收"></el-option>
          <el-option label="已拒绝" value="已拒绝"></el-option>
          <el-option label="已取消" value="已取消"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="始发地" prop="sfd" >
        <el-select v-model="queryParams.sfd" clearable placeholder="请选择">
          <el-option label="河南" value="河南"></el-option>
          <el-option label="河北" value="河北"></el-option>
          <el-option label="石家庄" value="石家庄"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目的地" prop="mdd" >
        <el-select v-model="queryParams.mdd" clearable placeholder="请选择">
          <el-option label="北京" value="北京"></el-option>
          <el-option label="上海" value="上海"></el-option>
          <el-option label="广州" value="广州"></el-option>
        </el-select>
      </el-form-item><br>

      <el-form-item label="下单时间">
        <el-date-picker
          v-model="daterangeTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="" prop="kcname">
        <el-input
          v-model="queryParams.kcname"
          placeholder="请输入关键字搜索"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>



    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="${comment}" align="center" prop="id" />-->
     <el-table-column label="订单号" align="center" prop="ddh" width="120" >
       <template slot-scope="scope">
         <el-link @click="handleUpdate(scope.row)" type="primary">{{scope.row.ddh}}</el-link>
       </template>
     </el-table-column>
<!--      <el-table-column label="关联运单号" align="center" prop="" width="120" />-->
      <el-table-column label="始发地" align="center" prop="sfd" />
      <el-table-column label="目的地" align="center" prop="mdd"/>
      <el-table-column label="客户名称" align="center" prop="kcname" width="180" />
      <el-table-column label="订单状态" align="center" prop="status"  />
      <el-table-column label="发货信息"  prop="fname" width="180">
        <template slot-scope="scope">
          <div>
            <span>姓名: {{ scope.row.fname }}</span><br>
            <span>电话: {{ scope.row.fphone }}</span><br>
            <span>地址: {{ scope.row.faddress }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="收货信息"  prop="sname" width="180">
        <template slot-scope="scope">
          <div>
            <span>姓名: {{ scope.row.sname }}</span><br>
            <span>电话: {{ scope.row.sphone }}</span><br>
            <span>地址: {{ scope.row.saddress }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="预计发货时间" align="center" prop="ktime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ktime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计收货时间" align="center" prop="stime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="订单类型" align="center" prop="ddlx" />
      <el-table-column label="货物类型" align="center" prop="hwlx" />
      <el-table-column label="货物数量" align="center" prop="hwnum" />
      <el-table-column label="总体积" align="center" prop="ztj" />
      <el-table-column label="总重量" align="center" prop="zl" />
      <el-table-column label="运费合计" align="center" prop="yfhj" />
      <el-table-column label="订单来源" align="center" prop="laiyaun" />
      <el-table-column label="订单备注" align="center" prop="bz" />
      <el-table-column label="下单时间" align="center" prop="time" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="cjr" />
      <el-table-column label="创建时间" align="center" prop="cjtime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cjtime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['goods:goods:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['goods:goods:edit']"
          >调度</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="hedan(scope.row)"-->
<!--            v-hasPermi="['goods:goods:edit']"-->
<!--          >取消合单</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单号" prop="ddh">
          <el-input v-model="form.ddh" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="客户名称" prop="kcname">
          <el-input v-model="form.kcname" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="发货联系人姓名" prop="fname">
          <el-input v-model="form.fname" placeholder="请输入发货联系人姓名" />
        </el-form-item>
        <el-form-item label="发货电话" prop="fphone">
          <el-input v-model="form.fphone" placeholder="请输入发货电话" />
        </el-form-item>
        <el-form-item label="发货地址" prop="faddress">
          <el-input v-model="form.faddress" placeholder="请输入发货地址" />
        </el-form-item>
        <el-form-item label="收货联系人姓名" prop="sname">
          <el-input v-model="form.sname" placeholder="请输入收货联系人姓名" />
        </el-form-item>
        <el-form-item label="收货电话" prop="sphone">
          <el-input v-model="form.sphone" placeholder="请输入收货电话" />
        </el-form-item>
        <el-form-item label="收货地址" prop="saddress">
          <el-input v-model="form.saddress" placeholder="请输入收货地址" />
        </el-form-item>
        <el-form-item label="预计发货时间" prop="ktime">
          <el-date-picker clearable
            v-model="form.ktime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择预计发货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="预计收货时间" prop="stime">
          <el-date-picker clearable
            v-model="form.stime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择预计收货时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="订单类型" prop="ddlx">
          <el-input v-model="form.ddlx" placeholder="请输入订单类型" />
        </el-form-item>
        <el-form-item label="货物类型" prop="hwlx">
          <el-input v-model="form.hwlx" placeholder="请输入货物类型" />
        </el-form-item>
        <el-form-item label="货物数量" prop="hwnum">
          <el-input v-model="form.hwnum" placeholder="请输入货物数量" />
        </el-form-item>
        <el-form-item label="总体积" prop="ztj">
          <el-input v-model="form.ztj" placeholder="请输入总体积" />
        </el-form-item>
        <el-form-item label="总重量" prop="zl">
          <el-input v-model="form.zl" placeholder="请输入总重量" />
        </el-form-item>
        <el-form-item label="运费合计" prop="yfhj">
          <el-input v-model="form.yfhj" placeholder="请输入运费合计" />
        </el-form-item>
        <el-form-item label="订单来源" prop="laiyaun">
          <el-input v-model="form.laiyaun" placeholder="请输入订单来源" />
        </el-form-item>
        <el-form-item label="订单备注" prop="bz">
          <el-input v-model="form.bz" placeholder="请输入订单备注" />
        </el-form-item>
        <el-form-item label="下单时间" prop="time">
          <el-date-picker clearable
            v-model="form.time"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择下单时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="创建人" prop="cjr">
          <el-input v-model="form.cjr" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="创建时间" prop="cjtime">
          <el-date-picker clearable
            v-model="form.cjtime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="操作确认"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <span>确定取消已合并的订单吗？</span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="queding()">确 定</el-button>
  </span>
    </el-dialog>
    <el-dialog
      title="操作确认"
      :visible.sync="dialogVisible1"
      width="30%"
      :before-close="handleClose">
      <span>确定合并选中的订单吗？</span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible1 = false">取 消</el-button>
    <el-button type="primary" @click="queding1()">确 定</el-button>
  </span>
    </el-dialog>
    <el-dialog
      title="拆单"
      :visible.sync="dialogVisible2"
      width="40%"
      :before-close="handleClose">
      <el-table
        :data="tableData"
        style="width: 100%">
        <el-table-column
          prop="id"
          label="序号">
        </el-table-column>
        <el-table-column
          prop="name"
          label="货物名称">
        </el-table-column>
        <el-table-column
          prop="num"
          label="数量">
        </el-table-column>
        <el-table-column
          prop="i"
          label="长*宽*高(m)">
        </el-table-column>
        <el-table-column
          prop="tiji"
          label="体积(m3)">
        </el-table-column>
        <el-table-column
          prop="zhongliang"
          label="重量(kg)">
        </el-table-column>
        <el-table-column
          prop="address"
          label="拆单数量">
          <template slot-scope="scope">
            <el-input v-model="scope.row.address" placeholder="请输入拆单数量" style="width: 100px"></el-input>
            </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible2 = false">取 消</el-button>
    <el-button type="primary" @click="queding2()">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import { listGoods, getGoods, delGoods, addGoods, updateGoods } from "@/api/goods/goods";

export default {
  name: "Goods",
  data() {
    return {
      address: '',
      dialogVisible: false,
      dialogVisible1: false,
      dialogVisible2: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sfd: null,
        mdd: null,
        kcname: null,
        status: null,
        time: null,
        num: '',
      },
      daterangeTime: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      tableData: [{}]
    };


  },
  created() {
    if (this.$route.query.status != null && this.$route.query.status != undefined) {
      this.queryParams.status = this.$route.query.status;
      this.getList();
    }
    this.getList();
  },
  methods: {
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeTime && '' != this.daterangeTime) {
        this.queryParams.params["beginTime"] = this.daterangeTime[0];
        this.queryParams.params["endTime"] = this.daterangeTime[1];
      }
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    dayin() {
      window.print();
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        ddh: null,
        sfd: null,
        mdd: null,
        kcname: null,
        status: null,
        fname: null,
        fphone: null,
        faddress: null,
        sname: null,
        sphone: null,
        saddress: null,
        ktime: null,
        stime: null,
        ddlx: null,
        hwlx: null,
        hwnum: null,
        ztj: null,
        zl: null,
        yfhj: null,
        laiyaun: null,
        bz: null,
        time: null,
        cjr: null,
        cjtime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    hedan() {
      this.dialogVisible = true;
    }, hd() {
      this.dialogVisible1 = true;
    },
    chaidan() {
      this.dialogVisible2 = true;
    },
    queding() {
      this.dialogVisible = false;
      this.$message.success('取消合单成功');
    },
    queding1() {
      if (this.ids.length == 0) {
        this.$message.error('请选择要合并的订单');
        return;
      }
      if (this.ids.length < 2) {
        this.$message.error('请选择两个以上订单进行合并');
        return;
      }
      if (this.status != '待审核') {
        this.$message.error('只有待审核订单才能进行合并');
        return;
      }
      this.dialogVisible1 = false;
      this.$message.success('合单成功');



  },
  queding2() {
    if (this.address == 0) {
      this.$message.error('请输入拆单数量');
      return;
    }
    this.dialogVisible2 = false;
    this.$message.success('拆单成功');
  },


  /** 新增按钮操作 */
  handleAdd() {
    // this.reset();
    // // this.open = true;
    // this.title = "添加订单";
    // location.href = '/kaidan'
    this.$router.push({path: '/goods/kaidan'})

  },
  /** 修改按钮操作 */
  handleUpdate(row) {
    // this.reset();
    const id = row.id || this.ids
    this.$router.push({path: '/goods/kaidan', query: {id: id}})

  },
  /** 提交按钮 */
  // submitForm() {
  //   this.$refs["form"].validate(valid => {
  //     if (valid) {
  //       if (this.form.id != null) {
  //         updateGoods(this.form).then(response => {
  //           this.$modal.msgSuccess("修改成功");
  //           this.open = false;
  //           this.getList();
  //         });
  //       } else {
  //         addGoods(this.form).then(response => {
  //           this.$modal.msgSuccess("新增成功");
  //           this.open = false;
  //           this.getList();
  //         });
  //       }
  //     }
  //   });
  // },
  /** 删除按钮操作 */
  handleDelete(row) {
    const ids = row.id || this.ids;
    this.$modal.confirm('是否确认删除订单编号为"' + ids + '"的数据项？').then(function () {
      return delGoods(ids);
    }).then(() => {
      this.getList();
      this.$modal.msgSuccess("删除成功");
    }).catch(() => {
    });
  },
  /** 导出按钮操作 */
  handleExport() {
    this.download('goods/goods/export', {
      ...this.queryParams
    }, `goods_${new Date().getTime()}.xlsx`)
  }
}
};
</script>
